<template>
  <el-form :model="formData" :rules="rules" ref="form" class="layout">
    <div class="user">
      <div style="display: flex; flex-direction: column;">
        <el-form-item prop="oldPassword" label="原密码" :rules="[{ required: true, message: '原密码不能为空', trigger: 'blur' }]">
          <el-input type="password" v-model="formData.oldPassword" style="width: 350px;height: 30px;" />
        </el-form-item>
      </div>
    </div>
    <div class="user1">
      <div style="display: flex; flex-direction: column;">
        <el-form-item prop="newPassword" label="新密码" :rules="[{ required: true, message: '新密码不能为空', trigger: 'blur' }]">
          <el-input type="password" v-model="formData.newPassword" style="width: 350px;height: 30px;" />
        </el-form-item>
      </div>
    </div>
    <div class="use2">
      <div style="display: flex; flex-direction: column;">
        <el-form-item prop="confirmPassword" label="确认密码"
          :rules="[{ required: true, message: '确认密码不能为空', trigger: 'blur' }]">
          <el-input type="password" v-model="formData.confirmPassword" style="width: 350px;height: 30px;" />
        </el-form-item>
      </div>
    </div>
    <div style="text-align: center; margin-top: 20px;">
      <el-button type="primary" @click="submitForm">修改</el-button>
    </div>
  </el-form>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { useUserStore } from '@/stores';
import { computed } from 'vue';
import { ElForm, ElMessage, ElMessageBox } from 'element-plus';
import { updateAdminInfo } from '@/api/My';
import { adminLogin } from '@/api/login'
import type { UserInfo } from '@/stores/store/user';

const userStore = useUserStore();
const userInfo = computed(() => userStore.getUserInfo());

//密码
const formData = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: '',
});
//表单
const userData = ref({
  username: userInfo.value?.username,
  password: '',
});
//验证规则
const rules = {
  oldPassword: [
    { required: true, message: '原密码不能为空', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '新密码不能为空', trigger: 'blur' },
  ],
  confirmPassword: [
    { required: true, message: '确认密码不能为空', trigger: 'blur' }
  ]
};

//提交
const submitForm = async () => {
  // 检查新密码和确认密码是否一致
  if (formData.value.newPassword !== formData.value.confirmPassword) {
    ElMessage.error('新密码与确认密码不一致！');
    return;
  }

  // 如果密码一致，继续执行提交操作
  ElMessageBox.confirm(
    '确认更改密码吗？',
    '确认提交',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(async () => {
    if (userData.value.username) {
      userData.value.password = formData.value.oldPassword;
      const response = await adminLogin(userData.value as UserInfo);
      if (response.data.code === 1) {
        userData.value.password = formData.value.newPassword;
        const response2 = await updateAdminInfo(userData.value as UserInfo);
        if (response2.data.code === 1) {
          ElMessage.success('密码更新成功');
        }
      } else {
        ElMessage.error('更新失败！');
      }
    }
  }).catch(() => {
    ElMessage.info('取消提交');
  });
}

</script>

<style lang="scss" scoped>
.layout {
  display: flex;
  flex-direction: column;
}

.pictur {
  margin-top: 50px;
  width: 90px;
  text-align: center;
  margin-left: 120px;
}

.user {
  display: flex;
  margin-left: 50px;
  align-items: center;
  margin-top: 20px;
}

.user1 {
  display: flex;
  margin-left: 50px;
  align-items: center;
}

.use2 {
  display: flex;
  margin-left: 37px;
  align-items: center;
}
</style>
